package net.sahv.bdyz.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 验证手机号 邮箱格式
 * 
 * @author renkaiwei
 *
 */
public class RegexValidateUtil {
	/**
	 * 验证邮箱
	 * 
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String email) {
		boolean flag = false;
		try {
			String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * 验证手机号码 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8],
	 * 18[0-9], 170[0-9] 移动号段:
	 * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,
	 * 178,1705 联通号段: 130,131,132,155,156,185,186,145,176,1709
	 * 电信号段:133,153,180,181,189,177,1700
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean checkMobileNumber(String mobileNumber) {
		boolean flag = false;
		if (mobileNumber.length() != 11) {
			return flag;
		}
		String MOBILE = "^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
	    /**
	     * 中国移动：China Mobile
	     * 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
	     */
		String CM = "(^1(3[4-9]|4[7]|5[0-27-9]|7[8]|8[2-478])\\d{8}$)|(^1705\\d{7}$)";
	    /**
	     * 中国联通：China Unicom
	     * 130,131,132,155,156,185,186,145,176,1709
	     */
		String CU = "(^1(3[0-2]|4[5]|5[56]|7[6]|8[56])\\d{8}$)|(^1709\\d{7}$)";
	    /**
	     * 中国电信：China Telecom
	     * 133,153,180,181,189,177,1700
	     */
		String CT = "(^1(33|53|77|8[019])\\d{8}$)|(^1700\\d{7}$)";
		try {
			Pattern regex1 = Pattern.compile(MOBILE);
			Matcher matcher1 = regex1.matcher(mobileNumber);
			boolean flag1 = matcher1.matches();
			Pattern regex2 = Pattern.compile(CM);
			Matcher matcher2 = regex2.matcher(mobileNumber);
			boolean flag2 = matcher2.matches();
			Pattern regex3 = Pattern.compile(CU);
			Matcher matcher3 = regex3.matcher(mobileNumber);
			boolean flag3 = matcher3.matches();
			Pattern regex4 = Pattern.compile(CT);
			Matcher matcher4 = regex4.matcher(mobileNumber);
			boolean flag4 = matcher4.matches();
			if (flag1 || flag2 || flag3 || flag4) {
				flag = true;
			}
		} catch (Exception e) {
			flag = false;
		}
		return flag;
	}
}
